#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

typedef int ElemType;
typedef struct LNode
{
	ElemType data;
	struct LNode* next;
}LinkNode;

bool DivideList(LinkNode*& L, ElemType x);	
bool InitList(LinkNode*& L);		
bool DispList(LinkNode* L);			

int main(int argc, const char* argv[])
{
	LinkNode* L = NULL;
	int x;
	bool flag = false;
	InitList(L);
	printf("请输入基准。\n");
	scanf("%d", &x);
	flag = DivideList(L, x);
	if (flag)
		printf("算法执行成功。\n");
	else
		printf("算法执行失败。\n");
	DispList(L);
	return 0;
}

bool InitList(LinkNode*& L)		
{
	while (!L) {
		L = (LinkNode*)malloc(sizeof(LNode));
	}
	L->next = NULL;
	int i, n;
	LinkNode* p = NULL, * q = NULL;
	q = L;
	printf("请输入数据规模：\n");
	scanf("%d", &n);
	printf("请输入数据：\n");
	for (i = 0; i < n; i++) {
		while (!p) {
			p = (LinkNode*)malloc(sizeof(LNode));
		}
		scanf("%d", &p->data);
		q->next = p;
		q = p;
		p = q->next = NULL;
	}
	return true;
}
bool DispList(LinkNode* L)		
{
	LinkNode* p = L->next;
	while (p) {
		printf("\t%d", p->data);
		p = p->next;
	}
	printf("\n");
	return true;
}
bool DivideList(LinkNode*& L, ElemType x)	
{
	if (L->next==NULL) {
		printf("单链表不存在。\n");
		return false;
	}
	LinkNode* p = NULL, * q = NULL, * r = NULL;	
	p = L；
	while (!q) {
		q = (LinkNode*)malloc(sizeof(LinkNode));
	}
	r = q;	
	while (p->next) {
		if (p->next->data < x) {
			r->next = p->next;		
			r=r->next;			
			p->next= p->next->next;	
			r->next = NULL;		
		}
		else
			p = p->next;		
	}
	r->next = L->next;	
	L->next = q->next;	
	free(q);			
	return true;
}
